package com.maxima.maximaAttendance.daos;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.maxima.maximaAttendance.pojos.TimeLog;
import com.maxima.maximaAttendance.pojos.User;
import com.maxima.maximaAttendance.utils.HibernateUtils;

public class TimeLogDAO {

	public int checkIfPresent(User user, Date date) {
		List<TimeLog> tlist = user.getTimeLogs();
		for (TimeLog timeLog : tlist) {
			if (timeLog.getRecordDate().equals(date))
				return timeLog.getId();
		}
		return -1;
	}

	public TimeLog getTimeLogByDate(User user, String date) {
		List<TimeLog> tlist = user.getTimeLogs();
		for (TimeLog timeLog : tlist) {
			if (timeLog.getRecordDate().equals(date))
				return timeLog;
		}
		return null;
	}

	public TimeLog getLastTimeLog(User user) {
		List<TimeLog> tlist = user.getTimeLogs();
		Date date = null;
		TimeLog t = null;
		for (TimeLog timeLog : tlist) {
			if (timeLog.getRecordDate() != null) {
				if (date != null) {
					Date curr = timeLog.getRecordDate();
					if (date.before(curr)) {
						date = curr;
						t = timeLog;
					}
				} else {
					date = timeLog.getRecordDate();
					t = timeLog;
				}
			}
		}
		return t;
	}

	public void addTimeOut(Integer id, Date logoutTime) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		TimeLog timeLog = null;
		try {

			timeLog = (TimeLog) session.createQuery(
					"select t from TimeLog t where t.id=" + id).uniqueResult();
			timeLog.setLogoutTime(logoutTime);
			session.save(timeLog);

		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
	}

	public void addNewTimeLog(TimeLog timeLog) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		try {
			session.save(timeLog);
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
	}

	public void deleteTimeLog(TimeLog timeLog) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		try {
			session.delete(timeLog);
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
	}

	public void setWorkFromHome(Integer id, Boolean status) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		TimeLog timeLog = null;
		try {

			timeLog = (TimeLog) session.createQuery(
					"select t from TimeLog t where t.id=" + id).uniqueResult();

			timeLog.setWorkFromHome(status);
			if (status) {
				timeLog.setComment("Work from home");
				session.save(timeLog);
			} else {
				timeLog.setComment("none");
			}

		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	public List<User> getUsersWorkingFromHomeByDate(Date date) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();

		List<User> users = new ArrayList<User>();
		List<TimeLog> tlist = null;
		try {
			tlist = (List<TimeLog>) session.createQuery(
					"select t from TimeLog t where recordDate='" + date + "'")
					.list();

		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		for (TimeLog timeLog : tlist) {
			if (timeLog.getWorkFromHome())
				users.add(timeLog.getUser());
		}
		// session.close();
		return users;
	}

	@SuppressWarnings("unchecked")
	public void setAllUsersWorkFromHomeByDate(Date date, Boolean status) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
//		List<TimeLog> tlist = null;
//		List<TimeLog> lst = new ArrayList<TimeLog>();
		String hql = "UPDATE TimeLog set workFromHome = :status "  + 
	             "WHERE recordDate = :date";
		Query query = session.createQuery(hql);
		query.setParameter("status", status);
		query.setParameter("date", date);
		int result = query.executeUpdate();
		System.out.println("Rows affected: " + result);
		
		hql = "DELETE FROM TimeLog "  + 
	             "WHERE recordDate = :date and loginTime is null";
		query = session.createQuery(hql);
		query.setParameter("date", date);
		result = query.executeUpdate();
		System.out.println("Rows affected: " + result);
		
//		try {
//
//			tlist = (List<TimeLog>) session.createQuery(
//					"select t from TimeLog t where recordDate='" + date + "'")
//					.list();
//
//			for (TimeLog timeLog : tlist) {
//				timeLog.setWorkFromHome(status);
//				if (status)
//					timeLog.setComment("Work from home");
//				else {
//					timeLog.setComment("none");
//					if (timeLog.getLoginTime() == null)
//						lst.add(timeLog);
//				}
//			}
//
//		} catch (HibernateException e) {
//			e.printStackTrace();
//			session.getTransaction().rollback();
//		}
//		session.getTransaction().commit();
//		for (TimeLog timeLog : lst) {
//			deleteTimeLog(timeLog);
//		}
		session.close();
	}

	@SuppressWarnings("unchecked")
	public List<TimeLog> getTimeLogsForMonth(String month, String year) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		List<TimeLog> tlist = null;
		try {
			tlist = (List<TimeLog>) session.createQuery(
					"select t from TimeLog t where t.rec_date like '%" + month
							+ "-" + year + "' order by t.user").list();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		// session.close();
		return tlist;
	}
}
